//
//  oldActivityFirstViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/4/4.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldActivityFirstViewController.h"
#import "oldActivityFirstTableViewCell.h"
#import "oldActivityViewController.h"
#import "oldHistoryActivityViewController.h"
#import "oldFamilyChooseParentListTableViewController.h"

@interface oldActivityFirstViewController ()
@property (nonatomic, copy)NSArray *dataSource;
@end

static NSString *const identifier = @"oldActivityFirstTableViewCell";

@implementation oldActivityFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    [self rigisterCell:identifier];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *rightbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 20)];
    
    rightbutton.titleLabel.font = [UIFont systemFontOfSize:15];
    //[leftbutton setBackgroundColor:[UIColor blackColor]];
    
    [rightbutton setTitle:@"我的预约" forState:UIControlStateNormal];
    
    [rightbutton addTarget:self action:@selector(rightbtnTapped:) forControlEvents:UIControlEventTouchDown];
    
    UIBarButtonItem *rightitem = [[UIBarButtonItem alloc]initWithCustomView:rightbutton];
    
    self.navigationItem.rightBarButtonItem = rightitem;
}

- (void)rightbtnTapped:(id)sender
{
    oldFamilyChooseParentListTableViewController *vc = [oldFamilyChooseParentListTableViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - initData
- (void)initData
{
    [oldHub waiting:@"获取活动中"];
    
    WeakSelf
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"pageNum", @"10", @"pageSize", nil];
    
    [OLDNetWork httpRequestWithPostPut:URL_GetCategoryList params:params success:^(id data) {
        [oldHub hideHUD];
        [weakSelf initDataSource:data];
        [weakSelf.tableView reloadData];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < [data count]; i++) {
        oldActivity *activity = [[oldActivity alloc] initWithDictionary:data[i]];
        [array addObject:activity];
    }
    
    _dataSource = array;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_dataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    oldActivityFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldActivityFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.activity = _dataSource[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    oldActivityViewController *vc = [oldActivityViewController new];
    vc.activity = _dataSource[indexPath.row];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 185;
}

@end
